from typing import Any

import stripe

stripe.api_key = "sk_test_51Ogsw5B0IyqaSJBrwczlr820jnmvA1qQQGoLZ2XxOsIzikpmXo4pRLjw4XVMTEBR8DdVTYySiAv1XX53Zv5xqynF00GfMqttFd"


class StripeCustomerService:
    def __init__(self):
        pass

    def create_customer(self, email, user_id, name, description) -> str:
        """
        :param email:
        :param user_id:
        :param name:
        :param description:
        :return: stripe_customer_id
        """
        customer = stripe.Customer.create(
            email=email,
            name=name,
            description=description,
            metadata={"user_id": user_id}  # IceMail平台上的用户ID
        )
        return customer.id

    def get_customer(self, stripe_customer_id) -> dict[str, Any] | None:
        """
        :param email:
        :param user_id:
        :param name:
        :param description:
        :return: stripe_customer_id
        """
        customer = stripe.Customer.retrieve(stripe_customer_id)

        return {
            "stripe_customer_id": customer.id,
            "email": customer.email,
            "name": customer.name,
            "description": customer.description,
            "metadata": customer.metadata,
        } if customer else None

    def update_customer(self, stripe_customer_id, **kwargs):
        # 调用 modify 方法更新客户信息
        updated_customer = stripe.Customer.modify(
            stripe_customer_id,  # 要更新的客户 ID
            **kwargs  # 要更新的字段（动态参数）
        )
        return updated_customer

    def delete_customer(self, stripe_customer_id):
        # 调用 delete 方法删除客户
        deleted_customer = stripe.Customer.delete(stripe_customer_id)
        # 成功删除后会返回包含 deleted: True 的对象
        return deleted_customer.deleted

    def list(self, email=None):
        customers = stripe.Customer.list(
            email=email,
            limit=100,  # 每页最多返回10条（默认10，最大100）
            starting_after="cus_Swww7oRhzL3cDE"  # 分页参数，从指定ID之后开始查询（可选）
        )
        print(customers.has_more)
        return [{
            "stripe_customer_id": customer.id,
            "email": customer.email,
            "name": customer.name,
            "description": customer.description,
            "metadata": customer.metadata,
        } for customer in customers]


if __name__ == '__main__':
    service = StripeCustomerService()
    print("=================Test Create=================")
    stripe_customer_id = service.create_customer("user@example.com", "xxxx", "John Doe", "IceMail平台注册用户John Doe")
    print("stripe_customer_id:", stripe_customer_id)
    print("=================Test Get=================")
    customer = service.get_customer(stripe_customer_id)
    print(customer)
    print("=================Test Update=================")
    service.update_customer(stripe_customer_id, email="test2@baidu.com", name="<NAME>")
    customer = service.get_customer(stripe_customer_id)
    print("=================Test Delete=================")
    service.delete_customer(stripe_customer_id)
    print(customer)
    print("=================Test List=================")
    print(service.list())
